package com.alibaba.citrus.cr.forecast.repository.impl.converter;

import com.alibaba.citrus.cr.common.constant.OrderFeaturesKey;
import com.alibaba.citrus.cr.common.enums.ordercenter.SalesOrderReasonEnum;
import com.alibaba.citrus.cr.forecast.functions.forecastorder.constants.ForecastConstants;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crforecastsales.domian.forecastorder.dto.LoadForecastOrderList2Request;
import com.epoch.app.crforecastsales.domian.forecastorder.model.ForecastOrder;
import com.epoch.app.crforecastsales.domian.forecastorderline.model.ForecastOrderLine;
import com.epoch.app.crforecastsales.dto.response.QueryForecastLineResponse;
import com.epoch.app.crforecastsales.enums.ForecastStatusEnum;
import com.epoch.app.crforecastsales.forecastorderlineindex.dto.QueryForecastOrderRequest;
import com.epoch.app.crforecastsales.forecastorderlineindex.model.ForecastOrderLineIndex;
import com.epoch.app.crforecastsales.model.dto.PageQueryForecastLineRequest;
import com.epoch.app.crforecastsales.ofsforecastorderindex.model.OfsForecastOrderIndex;
import com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.crpricecenter.model.dto.CreditQueryRequest;
import com.epoch.app.crunifiedinventory.sdo.enums.TransportModeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author yuanDeng
 * @Date 2021/9/17 14:43
 **/
public class ForecastOrderRepositoryConverter {
    public static Log log = Log.getLogger(ForecastOrderRepositoryConverter.class);

    public static LoadForecastOrderList2Request toForecastOrderLoadListRequest(ForecastOrderSDO source) {
        if (source == null) {
            return null;
        }
        LoadForecastOrderList2Request target = new LoadForecastOrderList2Request();
        if (!ObjectUtils.isEmpty(source.getId())) {
            target.setId(Long.parseLong(source.getId()));
        }
        target.setBizCode(source.getBizCode());
        target.setBusinessCode(source.getBusinessCode());
        target.setOuterOrderId(source.getOuterOrderId());
        target.setOrderTag(source.getOrderTag());
        target.setCustomerCode(source.getCustomerCode());
        target.setCustomerName(source.getCustomerName());
        target.setOrganizationCode(source.getOrganizationCode());
        target.setOrganizationName(source.getOrganizationName());
        target.setDepartmentCode(source.getDepartmentCode());
        target.setDepartmentName(source.getDepartmentName());
        target.setProductGroupCode(source.getProductGroupCode());
        target.setProductGroupName(source.getProductGroupName());
        target.setSaleChannelCode(source.getSaleChannelCode());
        target.setSaleChannelName(source.getSaleChannelName());
        target.setChannelCode(source.getChannelCode());
        target.setChannelName(source.getChannelName());
        target.setShipAddressCode(source.getShipAddressCode());
        target.setShipAddressInfo(source.getShipAddressInfo());
        target.setReceiveAddressCode(source.getReceiveAddressCode());
        target.setReceiveDetailAddress(source.getReceiveDetailAddress());
        target.setContact(source.getContact());
        target.setContactPhone(source.getContactPhone());
        target.setTransportModeCode(source.getTransportModeCode());
        target.setTransportModeName(source.getTransportModeName());
        target.setCreateReason(source.getCreateReason());
        target.setRemark(source.getRemark());
        target.setWareHouseCode(source.getWareHouseCode());
        target.setWareHouseName(source.getWareHouseName());
        target.setOrderTotalFee(source.getOrderTotalFee());
        target.setTotalActivityFee(source.getTotalActivityFee());
        target.setTotalReplyAmount(source.getTotalReplyAmount());
        target.setTotalPredictAmount(source.getTotalPredictAmount());
        target.setCreditBalance(source.getCreditBalance());
        target.setCustomerReferenceNo(source.getCustomerReferenceNo());
        target.setCustomerExpectTDate(source.getCustomerExpectTDate());
        target.setReplyTime(source.getReplyTime());
        target.setReplyStatus(source.getReplyStatus());
        target.setRefuseStatus(source.getRefuseStatus());
        target.setRefuseTime(source.getRefuseTime());
        target.setStatus(source.getStatus());
        target.setConfirmTime(source.getConfirmTime());
        target.setConfirmStatus(source.getConfirmStatus());
        target.setDealStatus(source.getDealStatus());
        target.setFinishedTime(source.getFinishedTime());
        target.setIdemId(source.getIdemId());
        target.setVaildBeginTime(source.getVaildBeginTime());
        target.setValidEndTime(source.getValidEndTime());
        target.setGmtCreate(source.getGmtCreate());
        target.setGmtModified(source.getGmtModified());
        target.setCreatorId(source.getCreatorId());
//        target.setCreatorName(source.getCreatorName());
//        target.setFeatures(source.getFeatures());
        target.setStart(source.getStart());
        target.setLimit(source.getLimit());
        target.setDir("gmtCreate");
        target.setAsc(false);
        target.setChannelCodes(target.getChannelCodes());
        target.setSaleChannelCodes(target.getSaleChannelCodes());
        target.setProductGroupCodes(target.getProductGroupCodes());
        target.setDocReasons(target.getDocReasons());
        target.setDepartmentCodes(target.getDepartmentCodes());
//        target.setAsc();
//        target.set_fields();
        return target;
    }

    public static void buildCreditRequestList(Map<String, ForecastOrderLineIndex> mainOrderMap, List<CreditQueryRequest> creditQueryRequestList) {
        for (ForecastOrderLineIndex forecastOrderLineIndex : mainOrderMap.values()) {
            String organizationCode = forecastOrderLineIndex.getMainOrganizationCode();
            String customerCode = forecastOrderLineIndex.getMainCustomerCode();
            String channelCode = forecastOrderLineIndex.getMainChannelCode();
            String itemGroupCode = forecastOrderLineIndex.getMainProductGroupCode();
            CreditQueryRequest creditQueryRequest = new CreditQueryRequest();
            creditQueryRequest.setCustomerCode(customerCode);
            creditQueryRequest.setChannelCode(channelCode);
            creditQueryRequest.setInnerOrganizationCode(organizationCode);
            creditQueryRequest.setItemGroupCode(itemGroupCode);
            creditQueryRequestList.add(creditQueryRequest);
        }
    }

    public static CreditQueryRequest buildCreditQueryRequest(ForecastOrderSDO forecastOrderInfo) {
        CreditQueryRequest creditQueryRequest = new CreditQueryRequest();
        creditQueryRequest.setCustomerCode(forecastOrderInfo.getCustomerCode());
        creditQueryRequest.setChannelCode(forecastOrderInfo.getChannelCode());
        creditQueryRequest.setInnerOrganizationCode(forecastOrderInfo.getOrganizationCode());
        creditQueryRequest.setItemGroupCode(forecastOrderInfo.getProductGroupCode());
        return creditQueryRequest;
    }

    public static ForecastOrderSDO toForecastOrderSDO(ForecastOrder source, Map<String, String> enumMap) {
        if (source == null) {
            return null;
        }
        ForecastOrderSDO target = new ForecastOrderSDO();
        target.setId(source.getId().toString());
        target.setBizCode(source.getBizCode());
        target.setBusinessCode(source.getBusinessCode());
        target.setOuterOrderId(source.getOuterOrderId());
        target.setOrderTag(source.getOrderTag());
        target.setCustomerCode(source.getCustomerCode());
        target.setOrderType(enumMap.get(source.getOrderType()));
        target.setCustomerName(source.getCustomerName());
        target.setOrganizationCode(source.getOrganizationCode());
        target.setOrganizationName(source.getOrganizationName());
        target.setDepartmentCode(source.getDepartmentCode());
        target.setDepartmentName(source.getDepartmentName());
        target.setProductGroupCode(source.getProductGroupCode());
        target.setProductGroupName(source.getProductGroupName());
        target.setSaleChannelCode(source.getSaleChannelCode());
        target.setSaleChannelName(source.getSaleChannelName());
        target.setChannelCode(source.getChannelCode());
        target.setChannelName(source.getChannelName());
        target.setShipAddressCode(source.getShipAddressCode());
        target.setShipAddressInfo(source.getShipAddressInfo());
        target.setReceiveAddressCode(source.getReceiveAddressCode());
        target.setReceiveDetailAddress(source.getReceiveDetailAddress());
        target.setContact(source.getContact());
        target.setContactPhone(source.getContactPhone());
        target.setTransportModeCode(source.getTransportModeCode());
        target.setTransportModeName(source.getTransportModeName());
        target.setCreateReason(source.getCreateReason());
        target.setRemark(source.getRemark());
        target.setWareHouseCode(source.getWareHouseCode());
        target.setWareHouseName(source.getWareHouseName());
        target.setOrderTotalFee(source.getOrderTotalFee());
        target.setTotalActivityFee(source.getTotalActivityFee());
        target.setTotalReplyAmount(source.getTotalReplyAmount());
        target.setTotalPredictAmount(source.getTotalPredictAmount());
        target.setCreditBalance(source.getCreditBalance());
        target.setCustomerReferenceNo(source.getCustomerReferenceNo());
        target.setCustomerExpectTDate(source.getCustomerExpectTDate());
        target.setReplyTime(source.getReplyTime());
        target.setReplyStatus(source.getReplyStatus());
        target.setRefuseStatus(source.getRefuseStatus());
        target.setRefuseTime(source.getRefuseTime());
        target.setStatus(source.getStatus());
        target.setConfirmTime(source.getConfirmTime());
        target.setConfirmStatus(source.getConfirmStatus());
        target.setDealStatus(source.getDealStatus());
        target.setFinishedTime(source.getFinishedTime());
        target.setIdemId(source.getIdemId());
        target.setVaildBeginTime(source.getVaildBeginTime());
        target.setValidEndTime(source.getValidEndTime());
        target.setGmtCreate(source.getGmtCreate());
        target.setGmtModified(source.getGmtModified());
        target.setCreatorId(source.getCreatorId());
        target.setCreatorName(source.getCreatorName());
        target.setCreator(source.getCreator());
        target.setUpdater(source.getUpdater());
        return target;
    }

    public static ForecastOrderLineSDO toForecastOrderLineSDO(ForecastOrderLine source) {
        if (source == null) {
            return null;
        }
        ForecastOrderLineSDO target = new ForecastOrderLineSDO();
        target.setId(source.getId());
        target.setMainOrderBizId(source.getMainOrderBizId());
        target.setDealStatus(source.getDealStatus());
        target.setGmtCreate(source.getGmt_create());
        target.setGmtModified(source.getGmt_modified());
        target.setCreatorId(source.getCreatorId());
        target.setModifierId(source.getModifierId());
        target.setCustomerName(source.getCreatorName());
        target.setBizCode(source.getBizCode());
        target.setOuterLineId(source.getOuterLineId());
        target.setMainOrderId(source.getMainOrderId());
        target.setStatus(source.getStatus());
        target.setCollectStatus(source.getCollectStatus());
        target.setAllowCollectAmount(source.getAllowCollectAmount());
        target.setCreatorName(source.getCreatorName());
        target.setAnalysisReason(source.getAnalysisReason());
        target.setAnalysisTime(source.getAnalysisTime());
        target.setIdemId(source.getIdemId());
        target.setActivityName(source.getActivityName());
        target.setActivityCode(source.getActivityCode());
        target.setCustomerCode(source.getCustomerCode());
        target.setSerialNumber(source.getSerialNumber());
        target.setDepartmentCode(source.getDepartmentCode());
        target.setScItemId(source.getScItemId());
        target.setScItemTitle(source.getScItemTitle());
        target.setPredictReplyAmount(source.getPredictReplyAmount());
        target.setQuantity(source.getQuantity());
        target.setUnitPrice(source.getUnitPrice());
        target.setTotalFee(source.getTotalFee());
        target.setActivityPrice(source.getActivityPrice());
        target.setTotalActivityFee(source.getTotalActivityFee());
        target.setActualTotalReplyAmount(source.getActualTotalReplyAmount());
        target.setShouldPayFee(source.getShouldPayFee());
        target.setDiscountFee(source.getDiscountFee());
        target.setDetailRemark(source.getDetailRemark());
        target.setReplyStatus(source.getReplyStatus());
        target.setReplyTime(source.getReplyTime());
        target.setRefuseReason(source.getRefuseReason());
        target.setConfirmTime(source.getConfirmTime());
        target.setConfirmStatus(source.getConfirmStatus());
        target.setFinishedTime(source.getFinishedTime());
        target.setInventoryUnit(source.getInventoryUnit());
        target.setInventoryUnitCode(source.getInventoryUnitCode());
        target.setSaleUnit(source.getSaleUnit());
        target.setSaleUnitCode(source.getSaleUnitcode());
        target.setWarehouseArea(source.getWarehouseArea());
        target.setUnitConvert(source.getUnitConvert());
        target.setGoodsProductationTime(source.getGoodsProductationTime());
        target.setWarehouseLocation(source.getWarehouseLocation());
        target.setScItemOutCode(source.getScItemOutCode());
        target.setCategoryChildDesc(source.getCategoryChildDesc());
        target.setCategoryChildCode(source.getCategoryChildCode());
        target.setCategoryCode(source.getCategoryCode());
        target.setCategoryDesc(source.getCategoryDesc());
        try {
            if (StringUtils.isNotEmpty(source.getFeatures())) {
                JSONObject features = JSONObject.parseObject(source.getFeatures());
                target.setFeatures(features);
                if (features.containsKey(ForecastConstants.ACTIVITY_PRICE_REMARK)){
                    target.setActivityPriceRemark(features.getString(ForecastConstants.ACTIVITY_PRICE_REMARK));
                }

                // 一盘货新增字段
                if (features.containsKey(OrderFeaturesKey.SOURCE_UNIT)) {
                    target.setSourceUnit(features.getString(OrderFeaturesKey.SOURCE_UNIT));
                }
                if (features.containsKey(OrderFeaturesKey.SOURCE_QUANTITY)) {
                    target.setSourceQuantity(features.getInteger(OrderFeaturesKey.SOURCE_QUANTITY));
                }
                if (features.containsKey(OrderFeaturesKey.SOURCE_PRICE)) {
                    target.setSourcePrice(features.getBigDecimal(OrderFeaturesKey.SOURCE_PRICE));
                }
            } else {
                Map<String, Object> features = new HashMap<>();
                target.setFeatures(features);
            }
        } catch (Exception e) {
            log.error("解析features异常", e);
        }
        target.setSyncVersion(source.getSyncVersion());
        target.setTenantId(source.getTenantId());
        target.setExt(source.getExt());
        target.setExt1(source.getExt1());
        target.setExt2(source.getExt2());
        target.setExt3(source.getExt3());
        target.setExt4(source.getExt4());
        target.setExt5(source.getExt5());
        target.setSourceLineNum(source.getSourceLineNum());
        target.setShippingWarehouseId(source.getShippingWarehouseId());
        target.setShippingWarehouseName(source.getShippingWarehouseName());
        target.setShippingWarehouseCode(source.getShipWarehouseCode());
        target.setBizFeatures(source.getBizFeatures());

        // 一盘货新增字段
        target.setConfirmQuantity(source.getConfirmQuantity());
        target.setInnerProductCode(source.getInnerProductCode());
        target.setInnerProductName(source.getInnerProductName());
        target.setItemBarCode(source.getBarCode());

        return target;
    }

    public static ForecastOrderLine toForecastOrderLine(ForecastOrderLineSDO source) {
        if (source == null) {
            return null;
        }
        ForecastOrderLine target = new ForecastOrderLine();
        target.setGoodsProductationTime(source.getGoodsProductationTime());
        target.setMainOrderBizId(source.getMainOrderBizId());
        target.setModifierId(source.getModifierId());
        target.setModifierCode(source.getModofierCode());
        target.setModifierName(source.getModifierName());
        target.setId(source.getId());
        target.setDealStatus(source.getDealStatus());
//        target.setGmtCreate(source.getGmtCreate());
//        target.setGmtModified(source.getGmtModified());
        target.setGmt_create(source.getGmtCreate());
        target.setGmt_modified(source.getGmtModified());
        target.setCreatorId(source.getCreatorId());
//        target.setModifierId(source.getModifierId());
        target.setBizCode(source.getBizCode());
        target.setOuterLineId(source.getOuterLineId());
        target.setMainOrderId(source.getMainOrderId());
        target.setStatus(source.getStatus());
        target.setCollectStatus(source.getCollectStatus());
        target.setAllowCollectAmount(source.getAllowCollectAmount());
        target.setCreatorName(source.getCreatorName());
        target.setAnalysisReason(source.getAnalysisReason());
        target.setAnalysisTime(source.getAnalysisTime());
        target.setIdemId(source.getIdemId());
        target.setActivityName(source.getActivityName());
        target.setActivityCode(source.getActivityCode());
        target.setCustomerCode(source.getCustomerCode());
        target.setSerialNumber(source.getSerialNumber());
        target.setDepartmentCode(source.getDepartmentCode());
        target.setScItemId(source.getScItemId());
        target.setScItemTitle(source.getScItemTitle());
        target.setScItemOutCode(source.getScItemOutCode());
        target.setPredictReplyAmount(source.getPredictReplyAmount());
        target.setQuantity(source.getQuantity());
        target.setUnitPrice(source.getUnitPrice());
        target.setTotalFee(source.getTotalFee());
        target.setActivityPrice(source.getActivityPrice());
        target.setTotalActivityFee(source.getTotalActivityFee());
        target.setActualTotalReplyAmount(source.getActualTotalReplyAmount());
        target.setShouldPayFee(source.getShouldPayFee());
        target.setDiscountFee(source.getDiscountFee());
        target.setDetailRemark(source.getDetailRemark());
        target.setReplyStatus(source.getReplyStatus());
        target.setReplyTime(source.getReplyTime());
        target.setRefuseReasonCode(source.getRefuseReasonCode());
        target.setRefuseReason(source.getRefuseReason());
        target.setConfirmTime(source.getConfirmTime());
        target.setConfirmStatus(source.getConfirmStatus());
        target.setFinishedTime(source.getFinishedTime());
        target.setInventoryUnit(source.getInventoryUnit());
        target.setSaleUnit(source.getSaleUnit());
        target.setSaleUnitcode(source.getSaleUnitCode());
        target.setWarehouseArea(source.getWarehouseArea());
        target.setUnitConvert(source.getUnitConvert());
        target.setWarehouseLocation(source.getWarehouseLocation());
        target.setFeatures(JSONObject.toJSONString(source.getFeatures()));
        target.setSyncVersion(source.getSyncVersion());
        target.setTenantId(source.getTenantId());
        target.setExt(source.getExt());
        target.setExt1(source.getExt1());
        target.setExt2(source.getExt2());
        target.setExt3(source.getExt3());
        target.setExt4(source.getExt4());
        target.setExt5(source.getExt5());
        return target;
    }

    public static QueryForecastOrderRequest toQueryForecastOrderRequest(PageQueryForecastLineRequest source) {
        if (source == null) {
            return null;
        }
        QueryForecastOrderRequest target = new QueryForecastOrderRequest();
        target.setMainorderId(source.getMainorderId());
        //target.setOrganizationCode(source.getO);

        if (CollectionUtils.isNotEmpty(source.getMainStatuses())) {
            target.setMainstatuses(source.getMainStatuses());
        }

        target.setLimit(source.getLimit());
        target.setStart(source.getStart());
        target.setCustomerCode(source.getCustomerCode());
        target.setTransportModeCode(source.getTransportModeCode());
        target.setRefuseReason(source.getRefuseReason());
        target.setMainorderId(source.getMainorderId());
//        target.setOrganizationCode(source.getOrganizationCode());
        target.setCustomerCode(source.getCustomerCode());
        target.setProductCode(source.getProductCode());
        target.setOrderType(source.getOrderType());
        target.setReceiveDetailAddress(source.getReceiveDetailAddress());
        //target.setDepartmentCodes(source.getDepartmentCode());
        target.setDepartmentCodes(source.getDepartmentCodes());
        target.setCreatorId(source.getCreatorId());
        target.setStatusCode(source.getStatusCode());
        target.setAnalysisReasonCode(source.getAnalysisReasonCode());
        target.setCustomerReferenceNo(source.getCustomerReferenceNo());
        target.setModifierId(source.getModifierId());
        target.setProductGroupCodes(source.getProductGroupName());
        target.setSaleChannelCodes(source.getSaleChannelCode());
        target.setDocReason(source.getDocReason());
        target.setDocRemark(source.getDocRemark());
        target.setWareHouseCode(source.getWareHouseCode());
        target.setContactor(source.getContactor());
        target.setContactPhone(source.getContactPhone());
        if (!ObjectUtils.isEmpty(source.getGmtCreate())) {
            target.setGmtCreate(source.getGmtCreate().toString());
        }
        //target.setChannelCodes(source.getChannelCode());
        target.setChannelCodes(source.getChannelCodes());
        if (!ObjectUtils.isEmpty(source.getCustomerExpectTDate())) {
            target.setCustomerExpectTDate(source.getCustomerExpectTDate().toString());
        }
        if (!ObjectUtils.isEmpty(source.getValidDate())) {
            target.setValidDate(source.getValidDate().toString());
        }

        target.setDetailStatus(source.getDetailStatus());
        target.setProductGroupNames(source.getProductGroupName());
        target.setDealStatuses(source.getDealStatuses());
        target.setStatuses(source.getStatuses());

        //add by myw,add start and limit of query ,begin
        target.setStart(source.getStart());
        target.setStatuses(source.getStatuses());
        target.setLimit(source.getLimit());
//        target.setStartTime(source.getStartTime());
//        target.setEndTime(source.getEndTime());
        target.setReceiveAddressCode(source.getReceivedAddressCode());
        //add by myw,add start and limit of query ,end
        return target;
    }


//    public static QueryForecastOrderLineRequest toQueryForecastOrderLineRequest(PageQueryForecastLineRequest source) {
//        if (source == null) {
//            return null;
//        }
//        QueryForecastOrderLineRequest target = new QueryForecastOrderLineRequest();
//        target.setMainorderId(source.getMainorderId());
//        target.setIds(source.getIds());
//        //target.setOrganizationCode(source.getO);
//        target.setCustomerCode(source.getCustomerCode());
//        target.setTransportModeCode(source.getTransportModeCode());
//        target.setRefuseReason(source.getRefuseReason());
//        target.setMainorderId(source.getMainorderId());
////        target.setOrganizationCode(source.getOrganizationCode());
//        target.setCustomerCode(source.getCustomerCode());
//        target.setProductCode(source.getProductCode());
//        target.setOrderType(source.getOrderType());
//        target.setReceiveDetailAddress(source.getReceiveDetailAddress());
//        target.setDepartmentCodes(source.getDepartmentCode());
//        target.setCreatorId(source.getCreatorId());
//        target.setStatusCode(source.getStatusCode());
//        target.setAnalysisReasonCode(source.getAnalysisReasonCode());
//        target.setCustomerReferenceNo(source.getCustomerReferenceNo());
//        target.setModifierId(source.getModifierId());
//        target.setProductGroupCodes(source.getProductGroupName());
//        target.setSaleChannelCodes(source.getSaleChannelCode());
//        target.setDocReason(source.getDocReason());
//        target.setDocRemark(source.getDocRemark());
//        target.setWareHouseCode(source.getWareHouseCode());
//        target.setContactor(source.getContactor());
//        target.setContactPhone(source.getContactPhone());
//        if (!ObjectUtils.isEmpty(source.getGmtCreate())) {
//            target.setGmtCreate(source.getGmtCreate().toString());
//        }
//        target.setChannelCodes(source.getChannelCode());
//        if (!ObjectUtils.isEmpty(source.getCustomerExpectTDate())) {
//            target.setCustomerExpectTDate(source.getCustomerExpectTDate().toString());
//        }
//        if (!ObjectUtils.isEmpty(source.getValidDate())) {
//            target.setValidDate(source.getValidDate().toString());
//        }
//
//        target.setDetailStatus(source.getDetailStatus());
//        target.setProductGroupNames(source.getProductGroupName());
//        target.setDealStatuses(source.getDealStatuses());
//        target.setStatuses(source.getStatuses());
//        return target;
//    }

    public static QueryForecastLineResponse toQueryForecastLineResponse(ForecastOrderLineIndex source) {
        if (source == null) {
            return null;
        }
        QueryForecastLineResponse target = new QueryForecastLineResponse();
        try {
            if (!ObjectUtils.isEmpty(source.getSubId())) {
                target.setId(source.getSubId().toString());
            }
            target.setAppCreate(Boolean.FALSE);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(source.getSubOs())&&"app".equalsIgnoreCase(source.getSubOs())){
                target.setAppCreate(Boolean.TRUE);
            }

            target.setRefuseReason(source.getSubRefuseReason());
            target.setSerialNumber(source.getSubSerialNumber());
            target.setOrganizationName(source.getMainOrganizationName());
            target.setProductGroupName(source.getMainProductGroupName());
            if (!ObjectUtils.isEmpty(source.getSubGmtCreate())) {
                target.setGmtCreate(new Date(source.getSubGmtCreate()));
            }

            if (!ObjectUtils.isEmpty(source.getSubGmtModified())) {
                target.setGmtModified(new Date(source.getSubGmtModified()));
            }
            target.setDepartmentName(source.getMainDepartmentName());
            target.setCreator(source.getSubCreator());
            target.setUpdater(source.getSubUpdater());
            target.setBizCode(source.getMainBizCode());
            if (!ObjectUtils.isEmpty(source.getMainId())) {
                target.setMainOrderId(source.getMainId().toString());
            }

            if (!ObjectUtils.isEmpty(source.getSubStatus())) {
                target.setStatus(source.getSubStatus().intValue());
            }

            if (!ObjectUtils.isEmpty(target.getStatus())) {
                if (!ObjectUtils.isEmpty(ForecastStatusEnum.of(target.getStatus()))) {
                    target.setStatusText(ForecastStatusEnum.of(target.getStatus()).getDesc());
                }
            }

            if (!ObjectUtils.isEmpty(source.getSubGmtModified())) {
                target.setGmtMoidfied(new Date(source.getSubGmtModified()));
            }
            target.setWareHouseName(source.getMainWareHouseName());
            target.setCustomerCode(source.getMainCustomerCode());
            target.setObjectId(source.getSubScItemId());
            target.setObjectName(source.getSubScItemTitle());
//        target.setObjectType(source.get);
            if (!ObjectUtils.isEmpty(source.getSubQuantity())) {
                target.setForecastAmount(Integer.parseInt(source.getSubQuantity()));
            }

            if (!ObjectUtils.isEmpty(source.getSubPredictReplyAmount())) {
                target.setPredictReplyAmount(source.getSubPredictReplyAmount().intValue());
            }

            if (!ObjectUtils.isEmpty(source.getSubActualTotalReplyAmount())) {
                target.setActualReplyAmount(source.getSubActualTotalReplyAmount().toString());
            }

            if (!ObjectUtils.isEmpty(source.getSubUnitPrice())) {
                target.setUnitFee(source.getSubUnitPrice().intValue());
            }

            if (!ObjectUtils.isEmpty(source.getSubShouldPayFee())) {
                target.setShouldPayFee(source.getSubShouldPayFee().intValue());
            }

            if (!ObjectUtils.isEmpty(source.getSubDiscountFee())) {
                target.setDiscountFee(source.getSubDiscountFee().intValue());
            }

            target.setRemark(source.getSubDetailRemark());
            target.setMainRemark(source.getMainRemark());
            if (!ObjectUtils.isEmpty(source.getSubReplyStatus())) {
                target.setReplyStatus(source.getSubReplyStatus().intValue());
            }

            if (!ObjectUtils.isEmpty(source.getSubReplyTime())) {
                target.setReplyTime(new Date(source.getSubReplyTime()));
            }

            if (!ObjectUtils.isEmpty(source.getSubConfirmTime())) {
                target.setConfirmTime(new Date(source.getSubConfirmTime()));
            }
            if (!ObjectUtils.isEmpty(source.getSubConfirmStatus())) {
                target.setConfirmStatus(source.getSubConfirmStatus().intValue());
            }

            if (!ObjectUtils.isEmpty(source.getSubFinishedTime())) {
                target.setFinishedTime(source.getSubFinishedTime().intValue());
            }

            if (!ObjectUtils.isEmpty(source.getSubInventoryUnit())) {
                target.setInventoryUnit(source.getSubInventoryUnit());
            }

            if (!ObjectUtils.isEmpty(source.getSubSaleUnit())) {
                target.setSaleUnit(source.getSubSaleUnit());
            }

            if (!ObjectUtils.isEmpty(source.getSubUnitConvert())) {
                target.setUnitConvert(source.getSubUnitConvert());
            }

            if (!ObjectUtils.isEmpty(source.getSubWarehouseArea())) {
                target.setWarehouseArea(source.getSubWarehouseArea().intValue());
            }

            if (StringUtils.isNotEmpty(source.getSubWarehouseLocation())) {
                try {
                    target.setWarehouseLocation(Integer.parseInt(source.getSubWarehouseLocation()));
                } catch (Exception e) {
                    log.error("ForecastOrderRepositoryConverter#toQueryForecastLineResponse", e);
                }
            }
            Map<String,String> features= (Map) JSONObject.parseObject(source.getSubFeatures());
            if (Objects.isNull(features)) {
                features = new HashMap<>();
            }
            //subfeatures中可能会出现数组类型 转成string可能会出现问题 这里正对数组做下处理
            for (String key : features.keySet()) {
                Object obj = features.get(key);
                if (obj instanceof List) {
                    features.put(key,JSONObject.toJSONString(obj));
                }
            }
            //features中可能出现activityCheckParams的对象数据
            if (MapUtils.isNotEmpty(features)&&features.containsKey(ForecastConstants.ACTIVITY_CHECK_PARAMS)){
                features.put(ForecastConstants.ACTIVITY_CHECK_PARAMS,JSONObject.toJSONString(features.get(ForecastConstants.ACTIVITY_CHECK_PARAMS)));
            }
            JSONObject jsonObject = JSONObject.parseObject(source.getSubFeatures());
            JSONArray orderId = jsonObject.getJSONArray("orderId");
            if (orderId != null){
                StringBuilder sb = new StringBuilder();
                for (int i =0 ; i<orderId.size(); i++){
                    sb.append(orderId.get(i).toString()).append(",");
                }
                String orderIds = sb.substring(0, sb.length() - 1);
                features.put("orderId", orderIds);
            }
            target.setFeatures(features);
            if (!ObjectUtils.isEmpty(source.getSubSyncVersion())) {
                target.setSyncVersion(source.getSubSyncVersion().intValue());
            }


            target.setCreatorId(source.getSubCreator());
            target.setModifierId(source.getMainModifierId());
            target.setTenantId(source.getSubTenantId());
            //target.setObjectId();
            //target.setObjectName();
            //target.setObjectType();
            if (!ObjectUtils.isEmpty(source.getSubUnitConvert())) {
                target.setUnitConversion(Integer.parseInt(source.getSubUnitConvert()));
            }
            if (!ObjectUtils.isEmpty(source.getSubAnalysisTime())) {
                target.setAnalysisTime(new Date(source.getSubAnalysisTime()));
            }
            if (!ObjectUtils.isEmpty(source.getSubQuantity())) {
                target.setQuantity(Integer.parseInt(source.getSubQuantity()));
            }
            target.setActualTotalReplyAmount(ObjectUtils.isEmpty(source.getSubActualTotalReplyAmount())? 0:source.getSubActualTotalReplyAmount().intValue());

            if (!ObjectUtils.isEmpty(source.getSubUnitPrice())) {
                target.setUnitPrice(source.getSubUnitPrice().toString());
            }
            if (!ObjectUtils.isEmpty(source.getSubActivityPrice())) {
                target.setActivityPrice(source.getSubActivityPrice().toString());
            }
            target.setCustomerName(source.getMainCustomerName());
            //target.setFeatures();
            target.setScItemTitle(source.getSubScItemTitle());
            target.setAnalysisReason(source.getSubAnalysisReason());

            Long subQuantity = StringUtils.isEmpty(source.getSubQuantity())? 0L :Long.parseLong(source.getSubQuantity());
            Long replyed = source.getSubActualTotalReplyAmount() == null ? 0L : source.getSubActualTotalReplyAmount();
            Long delta = subQuantity- replyed;
            target.setDeal(delta.intValue());
            //库存
            //target.setAvailableInventory(source.get);
            target.setActivityCode(source.getSubActivityCode());
            target.setActivityName(source.getSubActivityName());
            target.setDetailRemark(source.getSubDetailRemark());
            target.setInvertoryUnit(source.getSubInventoryUnit());
            target.setWareHouseName(source.getMainWareHouseName());
            if (!ObjectUtils.isEmpty(source.getSubUnitPrice()) && !ObjectUtils.isEmpty(source.getSubQuantity())) {
                Long l = source.getSubUnitPrice() * Integer.parseInt(source.getSubQuantity());
                target.setUnitPriceTotal(l.toString());
            }

            if (!ObjectUtils.isEmpty(source.getSubActivityPrice()) && !ObjectUtils.isEmpty(source.getSubQuantity())) {
                Long l = source.getSubActivityPrice() * Integer.parseInt(source.getSubQuantity());
                target.setActivityPriceTotal(l.toString());
            }
            target.setOrderType(source.getMainOrderType());
            target.setSaleChannelName(source.getMainSaleChannelName());
            target.setDepartmentCode(source.getMainDepartmentCode());

            if (org.apache.commons.lang3.StringUtils.isBlank(source.getSubGoodsProductationTime())
                    || "0".equals(source.getSubGoodsProductationTime().trim())){
                target.setGoodsProductationTime(null);
            }else {
                try {
                    String time = new SimpleDateFormat("yyyy-MM-dd").format(new BigDecimal(source.getSubGoodsProductationTime()).longValue());
                    target.setGoodsProductationTime(time);
                } catch (NumberFormatException e) {
//                    e.printStackTrace();
                    log.info("生产日期转换异常"+source.getSubId() + ":"+source.getSubGoodsProductationTime(),e);
                }
            }

            target.setRefuseReason(source.getSubRefuseReason());
            target.setSaleChannelName(source.getMainSaleChannelName());
            target.setChannelName(source.getMainChannelName());
            target.setReceiveDetailAddress(source.getMainReceiveDetailAddress());
            target.setCreatorName(source.getMainCreatorName());
            target.setCustomerRefNo(source.getMainCustomerReferenceNo());
            target.setCustomerRefDate(source.getMainCustomerExpectTDate() == null ? null : new Date(source.getMainCustomerExpectTDate()));
            target.setGmtCreate(source.getMainGmtCreate() == null ? null : new Date(source.getMainGmtCreate()));
            target.setInvertoryUnit(source.getSubInventoryUnit());
            target.setUnitConvert(source.getSubUnitConvert());
            target.setSaleUnit(source.getSubSaleUnit());
            target.setProductGroupName(source.getMainProductGroupName());
            target.setRefuseReason(source.getSubRefuseReason());
            if (Objects.nonNull(source.getMainVaildBeginTime())) {
                target.setVaildBeginTime(new Date(source.getMainVaildBeginTime()));
            }
            target.setRefuseReason(source.getSubRefuseReason());
            target.setMaterialsName(source.getSubScItemTitle());
            target.setCategoryDesc(source.getSubCategoryDesc());
            target.setCategoryChildDesc(source.getSubCategoryChildDesc());
            target.setOrderBizId(source.getMainOrderBizId());
            target.setActivityPriceRemark(source.getSubActivityPriceRemark());
            target.setOuterLineId(source.getSubOuterLineId());
            target.setGoodsCode(source.getSubGoodsCode());
            target.setAddressCode(source.getMainReceiveAddressCode());
            target.setChannelCode(source.getMainChannelCode());
            target.setGoodsGroupCode(source.getMainProductGroupCode());
            //增加消息说明
            target.setAnalysisMessage(source.getSubAnalysisMessage());
            //增加主单补充备注
            target.setMainAddRemark(source.getMainAddRemark());



        } catch (Exception e) {
            log.error("Convert repository failed:" + e.getMessage(), e);
        }

        return target;
    }

    public static ForecastOrderSDO indexToQueryForecastSDO(OfsForecastOrderIndex source, Map<String, String> enumMap) {
        ForecastOrderSDO target = new ForecastOrderSDO();
        target.setProductGroupName(source.getMainProductGroupName());
        target.setCustomerName(source.getMainCustomerName());
        target.setOrderType(enumMap.get(source.getMainOrderType()));
        target.setDepartmentName(source.getMainDepartmentName());
        target.setGmtCreate(new Date(source.getMainGmtCreate()));
        target.setId(source.getMainId().toString());
        target.setSaleChannelName(source.getMainSaleChannelName());
        target.setCreator(source.getMainCreator());
        target.setOrganizationName(source.getMainOrganizationName());
        target.setCreatorName(source.getMainCreatorName());
        target.setReceiveAddressCode(source.getMainReceiveAddressCode());
        target.setReceiveDetailAddress(source.getMainReceiveDetailAddress());
        if (Objects.nonNull(source.getMainStatus())) {
            target.setStatus(source.getMainStatus().intValue());
        }
        // 因为数据库 中没有 未生效 状态 所以这里 回写未生效状态：生效时间大于当天的
//        if (source.getMainVaildBeginTime() >= tomorrowFirstTime.getTime()) {
//            target.setStatus(ForecastStatusEnum.NOTACTIVE.getNumValue());
//        }
        target.setOrderBizId(source.getMainOrderBizId());
        target.setBisUnitCode(source.getMainBisUnitCode());
        target.setReceiveDetailAddress(source.getMainReceiveDetailAddress());
        target.setChannelName(source.getMainChannelName());
        target.setRemark(source.getMainRemark());
        target.setRefuseStatus(source.getMainRefuseStatus());
        //是否小程序提报
        if (source.getMainOs() != null && "app".equals(source.getMainOs())) {
            target.setAppCreate(Boolean.TRUE);
        } else {
            target.setAppCreate(Boolean.FALSE);
        }
        target.setTransportModeCode(source.getMainTransportModeCode());
        if(StringUtils.isNotEmpty(source.getMainTransportModeCode())&&TransportModeEnum.of(source.getMainTransportModeCode())!=null) {
            target.setTransportModeName(TransportModeEnum.of(source.getMainTransportModeCode()).getDesc());
        }
        if (Objects.nonNull(source.getMainCustomerExpectTDate())){
            target.setCustomerExpectTDate(new Date(source.getMainCustomerExpectTDate()));
        }
        target.setCustomerCode(source.getMainCustomerCode());
        target.setProductGroupCode(source.getMainProductGroupCode());
        target.setProductGroupName(source.getMainProductGroupName());
        target.setChannelCode(source.getMainChannelCode());
        target.setChannelName(source.getMainChannelName());
        target.setCreateReason(source.getMainCreateReason());
        if(StringUtils.isNotEmpty(source.getMainCreateReason())) {
            SalesOrderReasonEnum reasonEnum = SalesOrderReasonEnum.of(source.getMainCreateReason());
            if (Objects.nonNull(reasonEnum)) target.setCreateReasonName(reasonEnum.getDesc());
        }
        target.setShippingWarehouseCode(source.getMainShippingWarehouseCode());
        target.setSourceOrderNo(source.getMainSourceOrderNo());
        target.setShippingWarehouseName(source.getMainShippingWarehouseName());
        target.setDeliveryFactoryCode(source.getMainDeliveryFactoryCode());
        target.setStockCode(source.getMainStockCode());
//        target.setCustomerPurchaseOrderNo(source.getMainCustomerPurchaseOrderNo());
        target.setCustomerPurchaseOrderNo(source.getMainCustomerReferenceNo());
        if(source.getMainCustomerPurchaseDate()!=null) {
            target.setCustomerPurchaseDate(new Date(source.getMainCustomerPurchaseDate()));
        }
        target.setFromSys(source.getMainFromSys());
        target.setBusinessCode(source.getMainBusinessCode());
        target.setBusinessUnitCode(source.getMainBusinessUnitCode());
        target.setOrganizationCode(source.getMainOrganizationCode());
        target.setCreatorId(source.getMainCreatorId());
        target.setModifierId(source.getMainModifierId());
        target.setModifierName(source.getMainModifierName());
        if(source.getMainGmtModified()!=null){
            target.setGmtModified(new Date(source.getMainGmtModified()));
        }
        target.setBusinessType(source.getMainBusinessType());
        target.setBizFeatures(source.getMainBizFeatures());

        target.setBusinessUnitName(source.getMainBusinessUnitName());
        target.setSaleChannelCode(source.getMainSaleChannelCode());
        target.setSaleChannelName(source.getMainSaleChannelName());
        target.setOuterOrderId(source.getMainOuterOrderId());
        target.setSalesOrganizationCode(source.getMainSalesOrganizationCode());
        //target.setCreateReason(source.getMainCreateReason());
        //target.setCreateReasonName(source.getMainCreateReasonName());
        target.setOrderTypeCode(source.getMainOrderTypeCode());
        target.setOrderTypeName(source.getMainOrderTypeName());
        target.setSalesDepartmentCode(source.getMainSalesDepartmentCode());
        target.setSalesDepartmentName(source.getMainSalesDepartmentName());
        target.setReceiveWarehouseCode(source.getMainReceiveWarehouseCode());
        target.setReceiveWarehouseName(source.getMainReceiveWarehouseName());
        target.setReceiveFactoryCode(source.getMainReceiveFactoryCode());
        target.setReceiveStockCode(source.getMainReceiveAddressCode());
        target.setReceiveStockName(source.getMainReceiveDetailAddress());
        target.setModifierCode(source.getMainModifierCode());
        if(source.getMainVaildBeginTime() != null) {
            target.setVaildBeginTime(new Date(source.getMainVaildBeginTime()));
        }
        target.setOrderCategoryCode(source.getMainOrderCategoryCode());
        target.setCustomerGrade(source.getMainCustomerGrade());
        target.setCustomerGroup(source.getMainCustomerGroup());
        target.setCustomerGroupName(source.getMainCustomerGroupName());
        target.setOrderChannelCode(source.getMainOrderChannelCode());
        target.setAutoFulfillment(source.getMainAutoFulfillment());
        target.setFulfillmentTypeCode(source.getMainFulfillmentTypeCode());
        target.setFulfillmentPriority(source.getMainFulfillmentPriority());
        if(org.apache.commons.lang3.StringUtils.isNotBlank(source.getMainOrderTag())) {
            String[] orderTagArray = source.getMainOrderTag().split(",");
            target.setOrderBizTag(Arrays.asList(orderTagArray));
        }
        target.setKaArea(source.getMainKaArea());
        target.setKaSystem(source.getMainKaSystem());
        if (Objects.nonNull(source.getMainSourceOrderDate())){
            target.setSourceOrderDate(new Date(source.getMainSourceOrderDate()));
        }
        target.setCreatorCode(source.getMainCreatorCode());
        target.setShipToPartyCode(source.getMainShipToPartyCode());
        target.setShipToPartyName(source.getMainShipToPartyName());
        return target;
    }

    public static ForecastOrderSDO indexToQueryForecastSDODegrade(OfsForecastOrderIndex source, Map<String, String> enumMap) {
        ForecastOrderSDO target = new ForecastOrderSDO();
        target.setProductGroupName(source.getMainProductGroupName());
        target.setOrderType(source.getMainOrderType());///
        target.setCustomerName(source.getMainCustomerName());
        target.setOrderType(enumMap.get(source.getMainOrderType()));
        target.setDepartmentName(source.getMainDepartmentName());
        target.setGmtCreate(new Date(source.getMainGmtCreate()));
        target.setId(source.getMainId().toString());
        target.setSaleChannelName(source.getMainSaleChannelName());
        target.setCreator(source.getMainCreator());
        target.setOrganizationName(source.getMainOrganizationName());
        target.setCreatorName(source.getMainCreatorName());
        target.setReceiveAddressCode(source.getMainReceiveAddressCode());
        target.setReceiveDetailAddress(source.getMainReceiveDetailAddress());
        if (Objects.nonNull(source.getMainStatus())) {
            target.setStatus(source.getMainStatus().intValue());
        }
        // 因为数据库 中没有 未生效 状态 所以这里 回写未生效状态：生效时间大于当天的
//        if (source.getMainVaildBeginTime() >= tomorrowFirstTime.getTime()) {
//            target.setStatus(ForecastStatusEnum.NOTACTIVE.getNumValue());
//        }
        target.setOrderBizId(source.getMainOrderBizId());
        target.setBisUnitCode(source.getMainBisUnitCode());
        target.setReceiveDetailAddress(source.getMainReceiveDetailAddress());
        target.setChannelName(source.getMainChannelName());
        target.setRemark(source.getMainRemark());
        target.setRefuseStatus(source.getMainRefuseStatus());
        //是否小程序提报
        String mainFeatures = source.getMainFeatures();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(mainFeatures)){
            JSONObject mainFeaturesJson = JSONObject.parseObject(mainFeatures);
            String mainOs = mainFeaturesJson.getString(ForecastConstants.OS);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(mainOs) && "app".equals(mainOs)) {
                target.setAppCreate(Boolean.TRUE);
            } else {
                target.setAppCreate(Boolean.FALSE);
            }
        }

        return target;
    }
}
